%这个octave/matlab代码演示了简易的透视
clc
clear

M = 15;
n = 4;

for i = 1:M
  for j = 1:M
    point_real(i,j).x = i - M/2; %create points in world
    point_real(i,j).y = j + 5;
    point_real(i,j).z = -5;

    point_screen(i,j).x = n*point_real(i,j).x/point_real(i,j).y; %create relavent points on screen
    point_screen(i,j).y = n;
    point_screen(i,j).z = n*point_real(i,j).z/point_real(i,j).y;
  endfor
end

figure()
hold on
axis equal
view(0,0)
xlabel('x')
ylabel('y')
zlabel('z')

scatter3(0,0,0);

[x y] = meshgrid(-5:0.5:5,n-4:0.5:n+1); %draw screen
z=zeros(size(x));
s = surf(x,y,z,'EdgeColor','none','FaceColor',[0.9 0.9 0.9]);
rotate(s,[1 0 0],90,[0,n,0]);

for i = 1:M
  j=i;
  line([point_real(i,1).x point_real(i,M).x], [point_real(i,1).y point_real(i,M).y], [point_real(i,1).z point_real(i,M).z],'color','r');
  line([point_real(1,j).x point_real(M,j).x], [point_real(1,j).y point_real(M,j).y], [point_real(1,j).z point_real(M,j).z],'color','b');

  line([point_screen(i,1).x point_screen(i,M).x], [point_screen(i,1).y point_screen(i,M).y], [point_screen(i,1).z point_screen(i,M).z],'color','r');
  line([point_screen(1,j).x point_screen(M,j).x], [point_screen(1,j).y point_screen(M,j).y], [point_screen(1,j).z point_screen(M,j).z],'color','b');
end

i = int32(rand()*M/2+1);
j = int32(rand()*M/2+1);
line([0 point_real(i,j).x], [0 point_real(i,j).y], [0 point_real(i,j).z],'color','k');


